package com.cloud.salon.client.business.service.impl;

import com.cloud.salon.client.business.repository.ReportRepository;
import com.cloud.salon.common.base.response.Res;
import com.cloud.salon.common.base.util.DateUtil;
import com.cloud.salon.common.base.util.ValueUtil;
import com.cloud.salon.common.base.vo.highcharts.HighChartsVO;
import com.cloud.salon.common.base.vo.highcharts.ParameterVO;
import com.cloud.salon.common.base.vo.highcharts.TitleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Description 报表处理
 * @Author wolf--LZH
 * @CreateDate 2019/5/13 0013 下午 6:13
 */
@Service
public class ReportServiceImpl {

    @Autowired
    private ReportRepository repository;

    /*
     * @Description 获取会员与非会员消费比例数据
     * @Author wolf--LZH
     */
    public Res accountConsumeProportion(ParameterVO parameterVO) {
        // 获取数据
        List<Map<String, String>> maps = repository.accountConsumeProportion(parameterVO.getStartDate(), parameterVO.getEndDate());

        // 读取数据
        List<Map<String, Object>> datas = new ArrayList<>();
        for (Map<String, String> map: maps) {
            Map<String, Object> data = new HashMap<>();

            data.put("name", Integer.valueOf(map.get("account")) == 0? "非会员": "会员");
            data.put("y", ValueUtil.valueOfInt(map.get("count") ));

            datas.add(data);
        }

        // 创建单系列数据Map
        Map<String, Object> seriesLine = new HashMap<>();
        seriesLine.put("name", "比例");
        seriesLine.put("colorByPoint", true);
        seriesLine.put("data", datas);

        // 建立整体数据列表
        List<Map<String, Object>> series = new ArrayList<>();
        series.add(seriesLine);

        // 建立视图返回VO
        HighChartsVO highChartsVO = new HighChartsVO();
        highChartsVO.setTitle(new TitleVO("会员与非会员的消费比例饼图"));
        highChartsVO.setSeries(series);

        return new Res(0, "查询成功", highChartsVO);
    }

    public Res accountregistrationTrend(ParameterVO parameterVO) {
        List<Map<String, String>> maps = repository.consumeTrend(parameterVO.getStartDate(), parameterVO.getEndDate());
        return new Res(0, "查询成功", maps);
    }

    public Res consumeTrend(ParameterVO parameterVO) {
        List<Map<String, String>> maps = repository.consumeTrend(parameterVO.getStartDate(), parameterVO.getEndDate());
        return new Res(0, "查询成功", maps);
    }

    public Res itemConsumeTrend(ParameterVO parameterVO) {
        // 获取数据
        List<Map<String, String>> maps = repository.itemConsumeTrend(parameterVO.getStartDate(), parameterVO.getEndDate(), parameterVO.getItemIdList());

        // 获取x轴
        List<String> domainList = DateUtil.getDomainStr(parameterVO.getStartDate(), parameterVO.getEndDate(), DateUtil.MONTH, "yyyy-MM");

        // 索引
        int currentItem = 0;
        int currentDate = 0;

        // 建立序列数据集合
        List<Map<String, Object>> series = new ArrayList<>();
        Map<String, Object> seriesLine = new HashMap<>();
        List<Integer> data = new ArrayList<>();

        // 循环映射数据
        for (Map<String, String> map: maps) {
            // 读取单个数据
            String name = map.get("name");
            int itemId = ValueUtil.valueOfInt(map.get("item"));
            int count = ValueUtil.valueOfInt(map.get("count"));
            String domain = map.get("domain");

            // 若为第一个数据, 则进行相应处理
            if (currentItem == 0) {
                currentItem = itemId;
                // 设置序列名称
                seriesLine.put("name", name);
            }
            // 该数据与上一数据不属于一个项目的数据
            else if (currentItem != itemId) {
                // 若当前遍历时间为数据时间之前,则添加空数据
                while (currentDate < domainList.size()) {
                    data.add(0);
                    currentDate++;
                }

                // 将数据填充到seriesLine中
                seriesLine.put("data", data);
                series.add(seriesLine);

                // 初始化数据
                currentDate = 0;
                seriesLine = new HashMap<>();
                data = new ArrayList<>();
                currentItem = itemId;

                seriesLine.put("name", name);
            }

            // 若当前遍历时间为数据时间之前,则添加空数据
            while (!domainList.get(currentDate).equals(domain)) {
                data.add(0);
                currentDate++;
            }

            // 添加当前数据的数据
            data.add(count);
            currentDate++;
        }

        // 若当前遍历时间为数据时间之前,则添加空数据
        while (currentDate < domainList.size()) {
            data.add(0);
            currentDate++;
        }

        // 填充最后的数据序列
        seriesLine.put("data", data);
        series.add(seriesLine);

        // 建立VO
        HighChartsVO highChartsVO = new HighChartsVO();
        highChartsVO.setAxis(1, "时间", domainList);
        highChartsVO.setTitle(new TitleVO("所选项目在时间域内的销售情况"));
        highChartsVO.setSeries(series);

        return new Res(0, "查询成功", highChartsVO);
    }
}
